<?php
namespace PublicNo\Controller;
use Common\Controller\CommonController;
/**
 * 关键字自动回复模块
 */
class PublicNoAutoReplyController extends CommonController {
	protected $_public_autoreply;
	protected $_public_no;
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		$this->_auth();
		
		$this->_moduleid = 110;
		$this->_public_autoreply = new \PublicNo\Model\PublicNoAutoReplyModel();
		$this->_public_no = new \PublicNo\Model\PublicNoModel();
	}
	
	
	/**
	 * 添加公众号自动回复
	 */
	public function addAutoReplyAction(){
		$publicnoid = (int)I('request.publicnoid');
		$keywords = I('request.keywords');
		$replycontent = I('request.replycontent');
		$this->_addAutoReply($publicnoid,$keywords,$replycontent);
	}
	
	private function _addAutoReply($publicnoid,$keywords,$replycontent){
		if(empty($publicnoid) || empty($keywords) || empty($replycontent)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		$data['publicnoid'] = $publicnoid;
		$data['keywords'] = $keywords;
		$data['replycontent'] = $replycontent;
		$ret = $this->_public_autoreply->addReply($data);
	
		if($ret){
			$key = \cachekey::$key['AUTO_REPLY_KEYWORD'] . $publicnoid . ":" . md5($keywords);
			$this->_mycache->set($key , $replycontent);
			
			$this->outPut();
		}else{
			$this->status = 1923;
			$this->msg = L('STATUS_1923');
			$this->outPut();
		}
		
	}
	
	
	
	/**
	 * 修改公众号自动回复
	 */
	public function updateAutoReplyAction(){
		$id = (int)I('request.id');
		$publicnoid = (int)I('request.publicnoid');
		$keywords = I('request.keywords');
		$replycontent = I('request.replycontent');
		$this->_updateAutoReply($id,$publicnoid,$keywords,$replycontent);
	}
	
	private function _updateAutoReply($id,$publicnoid,$keywords,$replycontent){
		if(empty($id) || empty($publicnoid) || empty($keywords) || empty($replycontent)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$replyInfo = $this->_public_autoreply->getReplyById($id , $publicnoid);
		if(empty($replyInfo)){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$data['id'] = $id;
		$data['publicnoid'] = $publicnoid;
		$data['keywords'] = $keywords;
		$data['replycontent'] = $replycontent;
		$ret = $this->_public_autoreply->updateReply($data);
		if($ret){
			$key = \cachekey::$key['AUTO_REPLY_KEYWORD'] . $publicnoid . ":" . md5($keywords);
			$this->_mycache->set($key , $replycontent);
			
			$this->outPut();
		}else{
			$this->status = 1924;
			$this->msg = L('STATUS_1924');
			$this->outPut();
		}
	
	}
	
	
	/**
	 * 删除公众号自动回复
	 */
	public function deleteReplyAction(){
		$id = I('request.id');
		$publicnoid = (int)I('request.publicnoid');
		$this->_deleteReply($id , $publicnoid);
	}
	
	private function _deleteReply($id , $publicnoid){
		if(empty($id) || empty($publicnoid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		$id = explode(",", $id);
		$replyInfos = $this->_public_autoreply->getReplyByIds($id , $publicnoid);
	
		$data['id'] = $id;
		$data['publicnoid'] = $publicnoid;
		$ret = $this->_public_autoreply->deleteReply($data);
		if($ret){
			foreach($replyInfos as $replyInfo){
				$keywords = $replyInfo['keywords'];
				$replycontent = $replyInfo['replycontent'];
				$key = \cachekey::$key['AUTO_REPLY_KEYWORD'] . $publicnoid . ":" . md5($keywords);
				$this->_mycache->rm($key);
			}
		}
		$this->outPut();
	}
	
	
	/**
	 * 公众号自动回复
	 */
	public function autoReplyAction(){
		$publicnoid = (int)I('request.publicnoid');
		$keywords = I('request.keywords');
		$this->_autoReply($publicnoid,$keywords);
	}
	
	private function _autoReply($publicnoid,$keywords){
		if(empty($publicnoid) || empty($keywords)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$replyInfo = $this->_public_autoreply->getReplyByKeywords($publicnoid, $keywords);
		if(!empty($replyInfo)){
			$publicNoInfo = $this->_public_no->getPublicNoInfoById($publicnoid);
			
			//公众号发自动回复消息给自己
			$msg['from_cust_name'] = $publicNoInfo['nickname'];
			$msg['from_cust_id'] = (int)$publicNoInfo['custid'];
			$msg['to_cust_id'] = (int)$this->_custInfo['custid'];
			$msg['type'] = 1;
			$msg['session_type'] = 1;
			$msg['session_id'] = (int)$publicNoInfo['custid'];
			$msg['content'] = $replyInfo['replycontent'];
			$msg['custtype'] = "3";
			$msg['operationType'] = 4;//公众号
			\im::sendMsg(3, array($this->_custInfo['custid']), array(), $msg);
		}
		
		$this->outPut();
	}
	
	
	/**
	 * 公众号自动回复列表
	 */
	public function autoReplyListAction(){
		$publicnoid = (int)I('request.publicnoid');
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$keywords = I('request.keywords');
		$this->_autoReplyList($publicnoid , $keywords , $pageIndex , $pageSize);
	}
	
	private function _autoReplyList($publicnoid , $keywords="" , $pageIndex=0 , $pageSize=0){
		if(empty($publicnoid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['publicnoid'] = $publicnoid;
		$data['keywords'] = $keywords;
		$replyList = $this->_public_autoreply->getReplyList($data);
		//$replyList = $publicNoList['data'];
		
		$this->data = $replyList;
		$this->outPut();
	}
	
	
	/**
	 * 公众号自动回复详情
	 */
	public function autoReplyInfoAction(){
		$id = (int)I('request.id');
		$publicnoid = (int)I('request.publicnoid');
		$this->_autoReplyInfo($id , $publicnoid);
	}
	
	private function _autoReplyInfo($id , $publicnoid){
		if(empty($id) || empty($publicnoid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$replyInfo = $this->_public_autoreply->getReplyById($id , $publicnoid);
		
		$this->data = $replyInfo;
		$this->outPut();
	}
	 
	

}
